package com.lxb.myforum.controller.admin.sysmgt


import javax.servlet.http.HttpServletRequest

import com.lxb.myforum.base.BaseController
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody

import com.lxb.myforum.pojo.Menu
import com.lxb.myforum.service.MenuService

@Controller("adminMenuCtrl")
@RequestMapping("/admin/menus")
class MenuController @Autowired
constructor(private val menuService: MenuService) : BaseController() {

    @GetMapping(value = ["/", ""])
    fun index(req: HttpServletRequest): String {
        val menu = menuService.findById(0L)
        req.setAttribute("menu", menu)
        return "/admin/sysmgt/menus/index"
    }

    /**
     * 一般就是根据父id找子dictionary（直接子children）
     * @param menu
     * @return
     */
    @GetMapping(value = ["/all", "/list"])
    @ResponseBody
    fun dicts(menu: Menu): List<Menu> {
        return menuService.findAll(menu)
    }

    @RequestMapping(value = ["", "/"], method = [RequestMethod.PUT, RequestMethod.POST])
    @ResponseBody
    fun saveMenu(req: HttpServletRequest, @ModelAttribute menu: Menu): Menu? {
        return menuService.save(menu)
    }
}
